/**
 * NEPOUZIVAME
 */

package cz.masa.FotoCAT;

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;

class ImgShowPan extends JPanel{
    private ImgPolozka img_pol;
    private BufferedImage bimg;
    private Image img;
    private JLabel jLabel1;

    public ImgShowPan(ImgPolozka p_img_pol){
        this.img_pol = p_img_pol;
        img = Toolkit.getDefaultToolkit().getImage(ImgOper.getFullPath(img_pol.getId(), true));
        setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
        ImageIcon imic = new ImageIcon(ImgOper.getFullPath(img_pol.getId(), true));
        img = imic.getImage();
        int wi = img.getWidth(this); 
        int he = img.getHeight(this);
        //setMinimumSize(new Dimension(wi,he));
        //setPreferredSize(new Dimension(wi,he));
        //setMaximumSize(new Dimension(wi,he));
        //jLabel1 = new JLabel(imic);
        //add(jLabel1,java.awt.BorderLayout.CENTER);
    }
    
    public void paintComponent(Graphics g) {
        Dimension dim = new Dimension();
        Graphics2D g2 = (Graphics2D) g;
        super.paintComponent(g2);
        getSize(dim);
        double scaleW = (double)dim.width/(double)img.getWidth(this);
        double scaleH = (double)dim.height/(double)img.getHeight(this);
        AffineTransform aft = new AffineTransform();
        double scale = scaleW;
        if(scaleH < scaleW) scale = scaleH;
        
        int imh = (int)(img.getHeight(this)*scale);
        int imw = (int)(img.getWidth(this)*scale);
        int wah = (int)(dim.height/scale);
        int waw = (int)(dim.width/scale);
        aft.setToScale(scale,scale);
        aft.translate((waw - img.getWidth(this))/2,(wah - img.getHeight(this))/2);
        g2.drawImage(img,aft,this);
    }
}
